const mongoose = require('mongoose')
const Normal = require('../../model/normal')
const Content = require('../../model/content')
const Reply = require('../../model/reply')
const Message = require('../../model/message')
module.exports = async (req, res) => {
    const { id, flag } = req.params
    const { username, email, content, pid, aid } = req.fields
    console.log(req.fields);
    console.log(req.params);
    if (flag == 0) {
        try {
            var num = await Normal.find({ email }).count()
            if (num == 1) {
                var { _id } = await Normal.findOne({ email })
                var a = await Normal.updateOne({ email }, { username })
                console.log(_id);
            } else {
                var data = await Normal.create({ email, username })
                console.log(data);
                var _id = data._id
                console.log(_id);
            }
            var data = await Content.create({
                author: _id,
                content,
                article: id,
            })
        } catch (err){
            console.log(err);
            return res.send({
                data: null,
                meta: {
                    status: 401,
                    msg: '添加失败'
                }
            })
        }
    } else if (flag == 1) {
        try {
            var num = await Normal.find({ email }).count()
            if (num == 1 ) {
                var { _id } = await Normal.findOne({ email })
                var a = await Normal.updateOne({ email }, { username })
            } else {
                var data = await Normal.create({ email, username })
                var _id = data._id
            }

            var b = await Reply.create({
                author: _id,
                content: content,
                reply: aid
            })
            var c = await Content.updateOne({ _id: pid }, {
                $push: {
                    child: b._id
                }
            })
        } catch (err) {
            console.log(err);
            return res.send({
                data: null,
                meta: {
                    status: 401,
                    msg: '添加失败'
                }
            })
        }
    } else {
        try {
            var num = await Normal.find({ email }).count()
            if (num == 1 ) {
                var { _id } = await Normal.findOne({ email })
                var a = await Normal.updateOne({ email }, { username })
            } else {
                var data = await Normal.create({ email, username })
                var _id = data._id
            }
            var b = await Reply.create({
                author: _id,
                content: content,
                reply: aid
            })
            const c = await Content.updateOne({ child: { $in: [pid] } }, { $push: { child: b._id } })
        } catch (err) {
            console.log(err);
            return res.send({
                data: null,
                meta: {
                    status: 401,
                    msg: '添加失败'
                }
            })
        }
    }
    res.send({
        data: null,
        meta: {
            status: 201,
            msg: '添加成功'
        }
    })
}